// getters
import ACCESS_ENUM from "@/access/accessEnum";
import { UserControllerService } from "../../generated/services/UserControllerService";
import store from "@/store/index";

const getters = {};

export default {
  namespaced: true,
  state: () => ({
    loginUser: {
      username: null,
      userRole: ACCESS_ENUM.NOT_LOGIN,
    },
  }),
  loginUser: {
    username: null,
    userRole: null,
  },
  getters,
  actions: {
    async getLoginUser({ commit, state }: any, payload: any) {
      // TODO 改为从远程请求获取登录
      const res = await UserControllerService.getLoginUserUsingGet();
      // console.log("获取到登录信息" + res.data?.userName + res.data?.userRole);
      if (res.code === 0) {
        commit("updateUser", res.data);
        //有bug。直接更新更新不了username
        state.loginUser.username = res.data?.userName;
      } else {
        commit("updateUser", {
          ...state.loginUser,
          userRole: ACCESS_ENUM.NOT_LOGIN,
        });
      }
    },
  },
  mutations: {
    updateUser(state: any, payload: any) {
      state.loginUser = payload;
    },
  },
};
